package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.rdb.api.EcElementStyleColorTypeRdbService;
import com.ndp.fb.rdb.api.EcElementStyleRdbService;
import com.ndp.fb.rdb.model.ectemplate.EcElementStyle;
import com.ndp.fb.rdb.model.ectemplate.EcElementStyleColorType;
import com.ndp.fb.rdb.model.ectemplate.EcElementStyleColorTypeShowResult;
import com.ndp.fb.walle.business.EcElementStyleService;
import org.springframework.stereotype.Component;

/**
 * Created by sisi.chen on 2016/7/12.
 */
@Component
public class EcElementStyleServiceImpl implements EcElementStyleService {
    @Reference
    private EcElementStyleRdbService ecElementStyleRdbService;

    @Reference
    EcElementStyleColorTypeRdbService ecElementStyleColorTypeRdbService;

    @Override
    public synchronized EcElementStyle saveNewElementStyle(String styleCode, Integer styleGroup) {
        //按照code查找
        EcElementStyle ecElementStyle = ecElementStyleRdbService.findByCode(styleCode);
        if (null == ecElementStyle){
            //如果该样式未存储过则存储该样式
            ecElementStyle = new EcElementStyle();
            String uCode = styleCode.toUpperCase();
            ecElementStyle.setCode(uCode);
            ecElementStyle.setStyleGroup(styleGroup);
            boolean isMajor = "F".equals(uCode.substring(0, 1));
            ecElementStyle.setEnName(isMajor ? (CommonConstants.TEMPLATE_STYLE_F_EN + styleCode) : (CommonConstants.TEMPLATE_STYLE_S_EN + styleCode));
            ecElementStyle.setName(isMajor ? (CommonConstants.TEMPLATE_STYLE_F + styleCode) : (CommonConstants.TEMPLATE_STYLE_S + styleCode));
            ecElementStyle.setSort(isMajor ? 1 : 2);
            Long id = ecElementStyleRdbService.save(ecElementStyle);
            ecElementStyle.setId(id);


            EcElementStyleColorType ecElementStyleColorType = new EcElementStyleColorType();
            ecElementStyleColorType.setElementColorTypeId(1L);
            ecElementStyleColorType.setElementStyleId(id.intValue());
            ecElementStyleColorTypeRdbService.save(ecElementStyleColorType);
        }
        return ecElementStyle;
    }
}
